/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DVDDetailUI.java
 *
 * Created on 2010.11.09., 13:24:45
 */
package workshopdesign.UI;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.border.TitledBorder;
import workshopdesign.Control.DVDController;
import workshopdesign.Model.DVD;

/**
 *
 * @author Dom
 */
public class DVDDetailUI extends javax.swing.JPanel {

    private enum State {

        Empty, Editing
    };
    private State state = State.Empty;
    private DVD dvd = null;
    private DVDController dvd_controller = DVDController.getDVDControl();

    private SimpleDateFormat sdf = (SimpleDateFormat) SimpleDateFormat.getInstance();

    private BaseUIController parent;

    /** Creates new form DVDDetailUI */
    public DVDDetailUI(BaseUIController parent, String title, boolean editable) {
        initComponents();

        this.parent = parent;

        this.setBorder(new TitledBorder(title));

        title_field.setEditable(editable);
        director_field.setEditable(editable);
        date_field.setEditable(editable);

        ok_button.setVisible(editable);
        cancel_button.setVisible(editable);

        clear_form();
    }

    public DVD getDvd() {
        return dvd;
    }

    public void setDvd(DVD dvd) {
        this.dvd = dvd;
        update_status();
        load_data();
    }

    private void create_dvd() {
        Date date = get_date_from_date_field();
        if ((date == null) || (title_field.getText().length() == 0)) {
            return;
        }

        dvd_controller.add_dvd(
                title_field.getText(),
                director_field.getText(),
                date);

        clear_form();
        parent.update_data();
    }

    private void save_dvd_data() {
        Date date = get_date_from_date_field();
        if (date == null) {
            return;
        }

        dvd_controller.update_dvd(
                dvd.getId(),
                title_field.getText(),
                director_field.getText(),
                date);
        load_data();
        parent.update_data();
    }

    private void clear_form() {
        title_field.setText("");
        director_field.setText("");
        date_field.setText("");
    }

    public void load_data() {
        if(dvd == null){
            clear_form();
            return;
        }
        title_field.setText(dvd.getTitle());
        director_field.setText(dvd.getDirector());
        date_field.setText(sdf.format(dvd.getPublicationDate()));
    }

    public void clear_dvd(){
        dvd = null;
        update_status();
        clear_form();
    }

    private void update_status() {
        state = dvd == null ? State.Empty : State.Editing;
        update_button_texts();
    }

    private void update_button_texts() {
        if (state == State.Empty) {
            ok_button.setText("Add");
            cancel_button.setText("Clear");
            return;
        }
        if (state == State.Editing) {
            ok_button.setText("Save");
            cancel_button.setText("Cancel");
            return;
        }
    }

    private Date get_date_from_date_field() {
        Date date = null;
        sdf.applyPattern("yyyy.MM.dd.");
        try {
            date = sdf.parse(date_field.getText());
        } catch (ParseException e) {
            JOptionPane.showMessageDialog(
                    this,
                    "Error: The entered date is in incorrect format.\nPlease use the following pattern: 'yyyy.MM.dd.'",
                    "Error parsing date!",
                    JOptionPane.ERROR_MESSAGE);
        }
        return date;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel7 = new javax.swing.JLabel();
        title_field = new javax.swing.JTextField();
        director_field = new javax.swing.JTextField();
        date_field = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        ok_button = new javax.swing.JButton();
        cancel_button = new javax.swing.JButton();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(workshopdesign.WorkshopDesignApp.class).getContext().getResourceMap(DVDDetailUI.class);
        setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("Form.border.title"))); // NOI18N
        setName("Form"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        title_field.setName("title_field"); // NOI18N

        director_field.setName("director_field"); // NOI18N

        date_field.setName("date_field"); // NOI18N

        jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N

        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        ok_button.setText(resourceMap.getString("ok_button.text")); // NOI18N
        ok_button.setName("ok_button"); // NOI18N
        ok_button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ok_buttonMouseClicked(evt);
            }
        });

        cancel_button.setText(resourceMap.getString("cancel_button.text")); // NOI18N
        cancel_button.setName("cancel_button"); // NOI18N
        cancel_button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cancel_buttonMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addComponent(jLabel9)
                    .addComponent(ok_button))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cancel_button, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(director_field, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)
                    .addComponent(title_field, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)
                    .addComponent(date_field, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(title_field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(director_field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(date_field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ok_button)
                    .addComponent(cancel_button)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void ok_buttonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ok_buttonMouseClicked
        if (state == State.Empty) {
            create_dvd();
            return;
        }
        if (state == State.Editing) {
            save_dvd_data();
            return;
        }
    }//GEN-LAST:event_ok_buttonMouseClicked

    private void cancel_buttonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cancel_buttonMouseClicked
        if (state == State.Empty) {
            clear_form();
            return;
        }
        if (state == State.Editing) {
            setDvd(null);
            parent.clear_selection();
            return;
        }
    }//GEN-LAST:event_cancel_buttonMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancel_button;
    private javax.swing.JTextField date_field;
    private javax.swing.JTextField director_field;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JButton ok_button;
    private javax.swing.JTextField title_field;
    // End of variables declaration//GEN-END:variables
}
